const mclient = {}

mclient.getOS = function () {
  var n = !!navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
	    var t = navigator.userAgent.indexOf('Android') > -1
	    var e = window.location.search
  return e = e.toLowerCase(), e.indexOf('os=android') > -1 ? 'android' : e.indexOf('os=ios') > -1 ? 'ios'
	    : n ? 'ios' : t ? 'android' : void 0
}

mclient.iosNative = function (n) {
  window.location.href = 'hybrid://nativechannel/' + encodeURI(n)
}

mclient.androidNative = function (n) {
  typeof window.stub !== 'undefined' && window.stub.jsCallMethod(n)
}

mclient.nativeMethod = function (n) {
  typeof n === 'undefined' && alert('param error')
  var t = JSON.stringify(n)
  mclient.getOS() == 'ios' ? mclient.iosNative(t) : mclient.getOS() == 'android' && mclient.androidNative(t)
}

export default mclient
